package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

/**
 * 用户业务组件
 */
public class UserServiceImpl extends BaseService implements IUserService {


    @Override
    public int add(UserBean user) {
        SqlSession session = this.getSession();
        try {
            //得到Mapper的实体类对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            mapper.add(user);
            //提交事务
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
            session.rollback();
        }finally {
            //关闭session
            session.close();
        }
        return user.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.del(id);

        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String pwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.update(id,pwd);

        session.commit();
        session.close();
    }

    @Override
    public List<UserBean> findAll() {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public List<UserBean> findByName(String name) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByName(name);
        session.close();
        return list;
    }

    @Override
    public List<UserBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByBirthday(startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public void addMore(List<UserBean> list) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.addMore(list);

        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delMore(idArray);

        session.commit();
        session.close();
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByItem(name,startDate,endDate);

        session.close();
        return list;
    }

    @Override
    public CutPageBean<UserBean> cutAll(int pageNO) {
        CutPageBean<UserBean> cutBean = new CutPageBean<UserBean>();
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        //得到当前页需要显示的数据
        List<UserBean>list =
                mapper.cutAllList((pageNO - 1) * PAGESIZE,PAGESIZE);
        //得到总记录数
        int count = mapper.cutAllCount();
        //得到总页数
        int totalPage = count % PAGESIZE == 0 ?
                count/PAGESIZE : count/PAGESIZE + 1;
        //将数据封装为分页对象
        cutBean.setList(list);
        cutBean.setCount(count);
        cutBean.setTotalPage(totalPage);

        session.close();
        return cutBean;
    }
}
